package com.jrtc.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrtc.base.entity.bo.Patient;
import com.jrtc.base.entity.bo.PatientAppointment;
import com.jrtc.base.config.dto.ResultDTO;
import com.jrtc.base.config.dto.ResultDTOBuilder;
import com.jrtc.base.entity.vo.PatientAppointmentVo;
import com.jrtc.base.entity.vo.ReserveVo;
import com.jrtc.service.PatientAppointmentService;
import com.jrtc.service.PatientService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 患者预约单表(PatientAppointment)表控制层
 *
 * @author makejava
 * @since 2022-03-21 17:19:03
 */
@RestController
@RequestMapping("reserve")
public class PatientAppointmentController {
    /**
     * 服务对象
     */
    @Resource
    private PatientAppointmentService patientAppointmentService;
    @Resource
    private PatientService patientService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public PatientAppointment selectOne(Integer id) {
        return this.patientAppointmentService.queryById(id);
    }
    
    @RequestMapping("getReserveList")
    public ResultDTO getPatientAppointmentList(@RequestBody PatientAppointment patientAppointment)
    {
        IPage<PatientAppointment> ipage = patientAppointmentService.queryPatientAppointmentList(patientAppointment);
        return ResultDTOBuilder.success(ipage);
    }

    @RequestMapping("insertReserve")
    public ResultDTO insertPatientAppointment(@RequestBody PatientAppointment patientAppointment)
    {
        PatientAppointment tmpPatientAppointment = patientAppointmentService.insert(patientAppointment);
        if (tmpPatientAppointment != null)
        {
           return ResultDTOBuilder.success("添加成功！");
        }
        return ResultDTOBuilder.failureMsg("添加失败！");
    }

    @RequestMapping("getReserveById")
    public ResultDTO getPatientAppointmentById(@RequestBody Map<String,Integer> map)
    {
        int patientAppointmentId = map.get("patientAppointmentId");
        PatientAppointment patientAppointment = patientAppointmentService.queryById(patientAppointmentId);
        if (patientAppointment==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        return ResultDTOBuilder.success(patientAppointment);
    }

    @RequestMapping("updateReserve")
    public ResultDTO updatePatientAppointment(@RequestBody PatientAppointment patientAppointment)
    {
        if (patientAppointment==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        if (patientAppointmentService.update(patientAppointment)!=null)
        {
            return ResultDTOBuilder.success("修改信息成功！");
        }
        return ResultDTOBuilder.failureMsg("修改信息失败！");
    }
    
    @RequestMapping("deleteReserve")
    public ResultDTO deletePatientAppointment(@RequestBody Map<String,Integer> map)
    {
        int patientAppointmentId = map.get("patientAppointmentId");
        if (patientAppointmentService.deleteById(patientAppointmentId))
        {
            return ResultDTOBuilder.success("删除成功！");
        }
        return ResultDTOBuilder.failureMsg("删除失败！");
    }

    /**
     * 患者预约列表
     * */
    @PostMapping("/selectAppointmentList")
    public ResultDTO selectPatientAppointmentList(HttpServletRequest request, @RequestBody Map<String, Object> map){
        Integer patientId;
        if (map.get("id") != null && !map.get("id").equals("")){
            patientId = Integer.parseInt(map.get("id").toString());
        }
        else {
            Patient patient;
            try {
                patient = patientService.getLoginPatient(request);
            } catch (Exception e){
                return null;
            }
            if (patient == null){
                return ResultDTOBuilder.failureMsg("登录已过期，请重新登录");
            }
            patientId = patient.getPatientId();
        }


        if (map.get("appointmentTime") != null && !map.get("appointmentTime").equals("") &&
                map.get("type") != null && !map.get("type").equals("")){
            String appointmentTime = map.get("appointmentTime").toString();
            String type = map.get("type").toString();
            List<PatientAppointmentVo> patientAppointmentVoList;
            try {
                patientAppointmentVoList = patientAppointmentService.selectPatientAppointmentList(appointmentTime, type, patientId);
            } catch (Exception e){
                return ResultDTOBuilder.failureMsg(e.getMessage());
            }

            if (patientAppointmentVoList != null && patientAppointmentVoList.size() > 0){
                return ResultDTOBuilder.success(patientAppointmentVoList);
            }
            else {
                return ResultDTOBuilder.failureMsg("没有您的预约记录");
            }
        }
        else {
            return  ResultDTOBuilder.failureMsg("缺少必要参数");
        }
    }

    /**
     * 患者创建新预约
     * */
    @PostMapping("/insertPatientAppointment")
    public ResultDTO insertPatientAppointment(HttpServletRequest request, @RequestBody ReserveVo reserveVo){
        Patient patient;
        try {
            patient = patientService.getLoginPatient(request);
        } catch (Exception e){
            return null;
        }

        if (patient == null){
            return ResultDTOBuilder.failureMsg("登陆已过期，请重新登录");
        }
        if (reserveVo != null){
            Integer patientId = patient.getPatientId();
            int i;
            try {
                i = patientAppointmentService.insertPatientAppointment(reserveVo, patientId);
            } catch (Exception e){
                return ResultDTOBuilder.failureMsg(e.getMessage());
            }
            if (i > 0){
                return ResultDTOBuilder.success("创建成功");
            }
            else {
                return ResultDTOBuilder.failureMsg("创建失败");
            }
        }
        else {
            return ResultDTOBuilder.failureMsg("缺少必要参数");
        }
    }
}